home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / mac / sftkisrc.hqx / SoftKiss.src.1.8 / driver_shell / driver_shell_lib.c next >
Text File  |  1993-03-19  |  4KB  |  211 lines

  1. /*
  2.  * driver_shell - code for dynamicly installing a driver
  3.  * at runtime
  4.  * SoftKiss
  5.  * by Aaron Wohl / N3LIW (aw0g+@andrew.cmu.edu) jul 1990
  6.  * 6393 Penn Ave #303
  7.  * Pittsburgh PA, 15206
  8.  * work: (412)-268-5032
  9.  * home: (412)-731-6159
  10.  */
  11.  
  12. #include "driver_shell.h"
  13. #include "sfk_os_preserve.h"
  14. #include <string.h>
  15.  
  16. /*
  17.  * prepare a driver template to be a certain driver
  18.  * returns true if successfull
  19.  */
  20. int sh_prepare(
  21.     driver_shell_header_pt ads,
  22.     unsigned char *aname,
  23.     short aFlags,
  24.     short aDelay,
  25.     long acall_me,
  26.     long aiba4,
  27.     long aiba5,
  28.     long ash_more)
  29. {
  30.     if(ads->call_me!='call')return FALSE;
  31.     if(ads->iba4   !='iba4')return FALSE;
  32.     if(ads->iba5   !='iba5')return FALSE;
  33.     if(aname[0]==0)return FALSE;
  34.     if(aname[0]>(ds_NAME_LEN-1))return FALSE;
  35.  
  36. /*the template was ok so now fill it in*/
  37.     memcpy(ads->drvrName,aname,aname[0]+1);
  38.     ads->call_me=acall_me;
  39.     ads->iba4=aiba4;
  40.     ads->iba5=aiba5;
  41.     ads->sh_more=ash_more;
  42.     ads->drvrFlags=aFlags;
  43.     ads->drvrDelay=aDelay;
  44.     return TRUE;
  45. }
  46.  
  47. /*
  48.  * return a5 for driver_shell to set when it calls us back
  49.  * a5 is used when this module is linked to a test application
  50.  * normaly we are part of a driver and a4 is used
  51.  */
  52. long sh_geta5()
  53. {
  54.     asm { move.l a5,d0 }
  55. }
  56.  
  57. /*
  58.  * return a4 for driver_shell to set when it calls us back
  59.  */
  60. long sh_geta4()
  61. {
  62.     asm { move.l a4,d0 }
  63. }
  64.  
  65. /*
  66.  * strip address that strips even before 32bit addressing is on
  67.  * so the address will still be valid later
  68.  */
  69. void *sh_strip_address(void *addr)
  70. {
  71.     asm {
  72.     move.l addr,d0
  73.     and.l Lo3Bytes,d0
  74.     }
  75. }
  76.  
  77. /*
  78.  * install the passed driver
  79.  * apple never got around to the glue for this call
  80.  * if they ever do this routine should be removed
  81.  */
  82. pascal void _DrvrInstall(void) = 0xa03d;
  83. short DrvrInstall(Handle drvrHandle, short dRef)
  84. {
  85.     asm {
  86.         move.w dRef,d0
  87.         move.l drvrHandle,a0
  88.         move.l (a0),a0
  89.     }
  90.     _DrvrInstall();
  91. }
  92.  
  93. /*
  94.  * remove the passed device driver
  95.  * apple never got around to the glue for this call
  96.  * if they ever do this routine should be removed
  97.  */
  98. pascal void _DrvrRemove(void) = 0xa03e;
  99. short DrvrRemove(short refNum)
  100. {
  101.     asm {
  102.         move.w refNum,d0
  103.         _DrvrRemove
  104.     }
  105. }
  106.  
  107. /*
  108.  * Drvr install doesn't do anything except fill in dCtlRefNum
  109.  * so follow up and copy over the drivers info.
  110.  * Handle passed in should be locked
  111.  */
  112. short Real_DrvrInstall(Handle DrvrHandle, short dRef)
  113. {
  114.     int oserr;
  115.     DCtlHandle dCtl;
  116.     driver_shell_header_pt ash= (driver_shell_header_pt)(*DrvrHandle);
  117.     if((oserr=DrvrInstall(DrvrHandle,dRef))!=0)
  118.         return oserr;
  119.     dCtl=GetDCtlEntry(dRef);
  120.     (*dCtl)->dCtlDriver=(Ptr) DrvrHandle;    /*use handle, tech note is wrong*/
  121.     (*dCtl)->dCtlStorage=(Handle) 0L;
  122.     (*dCtl)->dCtlDelay=ash->drvrDelay;
  123.     (*dCtl)->dCtlEMask=ash->drvrEMask;
  124.     (*dCtl)->dCtlFlags|=ash->drvrFlags;
  125.     return 0;
  126. }
  127.  
  128. /*
  129.  * a resource into the system heap by name
  130.  * returns nil if it can't be loaded
  131.  */
  132. Handle sh_read_in_sys_res(ResType id,unsigned char *name)
  133. {
  134.     Handle res=0;
  135.     int needs_release=TRUE;
  136.     if((res=GetNamedResource(id,name))==0)
  137.         return 0;
  138.     HUnlock(res);
  139.     if(MemError()!=0)
  140.         goto error_exit;
  141.     DetachResource(res);
  142.     if(ResError()!=0)
  143.         goto error_exit;
  144.     /*detached now so if dail need to dispose not release*/
  145.     needs_release=FALSE;
  146.     MoveHHi(res);
  147.     if(MemError()!=0)
  148.         goto error_exit;
  149.     HLock(res);
  150.     if(MemError()!=0)
  151.         goto error_exit;
  152.     return res;
  153.  
  154. error_exit:
  155.     if(needs_release)
  156.         ReleaseResource(res);
  157.     else
  158.         DisposHandle(res);
  159.     return 0;
  160. }
  161.  
  162. /*
  163.  * load the driver template into memory
  164.  * returns nil if it can't be loaded
  165.  */
  166. Handle sh_read_in_driver_shell(void)
  167. {
  168.     return sh_read_in_sys_res('PROC',"\pdriver_shell");
  169. }
  170.  
  171. /*
  172.  * return a duplicate of the passed handle
  173.  * the copy will be in the system heap
  174.  * and moved high and locked
  175.  * must be called with the system heap zone set
  176.  */
  177. static Handle sh_copy_templatei(Handle template)
  178. {
  179.     long template_size;
  180.     Handle result=0;
  181.     if(template==0)
  182.         return 0;
  183.     template_size=GetHandleSize(template);
  184.     if(template_size==0)
  185.         return 0;
  186.     if((result=NewHandleSys(template_size))==0)
  187.         return 0;
  188.     MoveHHi(result);
  189.     if(MemError()!=0)
  190.         goto error_exit;
  191.     HLock(result);
  192.     if(MemError()!=0)
  193.         goto error_exit;
  194.     memcpy(*result,*template,template_size);
  195.     return result;
  196.  
  197. error_exit:
  198.     DisposHandle(result);
  199.     return 0;
  200. }
  201.  
  202. /*
  203.  * return a duplicate of the passed handle
  204.  * the copy will be in the system heap
  205.  * and moved high and locked
  206.  */
  207. Handle sh_copy_template(Handle template)
  208. {
  209.     return (Handle)OSP_protected_call(OSP_sys,sh_copy_templatei,template,0,0,0);
  210. }
  211.